package com.example.monthseekbar;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

/**
 * Created by Tsing on 2020/9/25
 */
public class MonthSeekBar extends View {

    private int totalHeight = DensityUtil.dp2px(80);
    private int totalWidth = DensityUtil.dp2px(250);
    private float stepWidth;

    public MonthSeekBar(Context context) {
        super(context);
        init();
    }

    public MonthSeekBar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MonthSeekBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        normalPaint = new Paint();
        normalPaint.setAntiAlias(true);
        normalPaint.setColor(Color.parseColor("#b4d4ff"));
        selectPaint = new Paint();
        selectPaint.setAntiAlias(true);
        selectPaint.setColor(Color.parseColor("#3b82e4"));

        normalPaint.setStyle(Paint.Style.FILL);
        selectPaint.setStyle(Paint.Style.FILL);

        normalRadius = DensityUtil.dp2px(5);
        selectRadius = DensityUtil.dp2px(15);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        totalHeight = MeasureSpec.getSize(heightMeasureSpec);
        int mode = MeasureSpec.getMode(widthMeasureSpec);
        if (mode == MeasureSpec.EXACTLY) {
            totalWidth = MeasureSpec.getSize(widthMeasureSpec);
        }
        stepWidth = (totalWidth * 1.0f - 2 * selectRadius) / 11;
        setMeasuredDimension(totalWidth, totalHeight);
    }

    private Paint normalPaint;
    private Paint selectPaint;
    private float normalRadius;
    private float selectRadius;


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        for (int i = 0; i < 12; i++) {
            canvas.drawCircle(selectRadius + stepWidth * i,
                    totalHeight - selectRadius,
                    normalRadius,
                    normalPaint);
        }
        canvas.drawLine(selectRadius,
                totalHeight - selectRadius,
                totalWidth * 1.0f - selectRadius,
                totalHeight - selectRadius,
                normalPaint);
        normalPaint.setStrokeWidth(DensityUtil.dp2px(2));

        canvas.drawCircle(selectRadius + curPos * stepWidth,
                totalHeight - selectRadius,
                selectRadius,
                selectPaint);

        selectPaint.setTextSize(60);
        canvas.drawText(curPos + 1 + "月",
                curPos * stepWidth,
                totalHeight - 3 * selectRadius,
                selectPaint);
    }

    private int curPos = 0;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        super.onTouchEvent(event);
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_MOVE:

                if (event.getY() > totalHeight - 2 * selectRadius) {
                    curPos = (int) ((event.getX() - selectRadius) / stepWidth);
                    invalidate();
                }

                break;
        }
        return true;
    }
}
